added Feb 2001 SDK
[windows-sources.git] / shared source / wpf / src / host / dll / detours.cxx
blob48e60720e6b569882b96233f0064c92c2f1cd029
1 //+-----------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Description:
6 // [See .hxx]
7 //
8 //------------------------------------------------------------------------
10 #include "precompiled.hxx"
11 #include "Detours.hxx"
12 #include "..\Detours\Detours.h"
14 // The managed PresentationHostSecurityManager needs to convince the ClickOnce elevation prompt to use the
15 // browser's top-level window as the owner. The prompt dialog is created without an explicit owner, on its
16 // own thread. As a fallback, SWF.Form.ShowDialog() uses the active window as owner. But there is no other
17 // window on the dialog's thread. So, we fake it by detouring ::GetActiveWindow() and returning the browser's
18 // top-level window on the first call.
19 HWND g_fakeActiveWindow;
21 // DLL-exported. Called by PresentationHostSecurityManager.
22 void __stdcall SetFakeActiveWindow(HWND hwnd)
24 g_fakeActiveWindow = hwnd;
27 HWND (WINAPI *Detours::s_pfGetActiveWindow)() = &GetActiveWindow;
29 HRESULT Detours::Init()
31 HRESULT hr = S_OK;
33 CHECK_ERROR_CODE(DetourTransactionBegin());
34 CHECK_ERROR_CODE(DetourUpdateThread(GetCurrentThread()));
35 CHECK_ERROR_CODE(DetourAttach((void**)&s_pfGetActiveWindow, GetActiveWindowDetour));
36 CHECK_ERROR_CODE(DetourTransactionCommit());
38 Cleanup:
39 return hr;
42 void Detours::Uninit()
44 if(s_pfGetActiveWindow != GetActiveWindow) // detoured?
46 if(DetourTransactionBegin() == NOERROR &&
47 DetourUpdateThread(GetCurrentThread()) == NOERROR &&
48 DetourDetach((void**)&s_pfGetActiveWindow, GetActiveWindowDetour) == NOERROR)
50 DetourTransactionCommit();
52 else
54 ASSERT(false);
59 HWND Detours::GetActiveWindowDetour()
61 if(g_fakeActiveWindow)
63 HWND hwnd = g_fakeActiveWindow;
64 g_fakeActiveWindow = 0;
65 return hwnd;
67 return (*s_pfGetActiveWindow)(); // call the real function